package com.jic.product.impl;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.product.entity.CategoryPropertyName;
import com.jic.product.entity.ProductPropertyName;
import com.jic.product.entity.ProductPropertyValue;
import com.jic.product.manage.request.ProductPropertyNameEditRequest;
import com.jic.product.manage.request.ProductPropertyValueEditRequest;
import com.jic.product.manage.request.ProductPropertyValueRequest;
import com.jic.product.manage.response.ProductPropertyNameResponse;
import com.jic.product.mapper.CategoryPropertyNameMapper;
import com.jic.product.mapper.ProductPropertyNameMapper;
import com.jic.product.mapper.ProductPropertyValueMapper;
import com.jic.product.service.ProductPropertyNameService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 商品属性名称表
 *
 * @author :
 * @version : v1.0.0
 * @email :
 * @since : 2020-01-10 07:03:01
 */
@Service
@Slf4j
public class ProductPropertyNameServiceImpl implements ProductPropertyNameService {

    @Autowired
    private ProductPropertyNameMapper productPropertyNameMapper;
    @Autowired
    private ProductPropertyValueMapper productPropertyValueMapper;
    @Autowired
    private CategoryPropertyNameMapper categoryPropertyNameMapper;


    /*
     * 商品属性新增
     * */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResult insert(ProductPropertyNameEditRequest productPropertyNameEditRequest) {
        RestResult result = new RestResult();
        if (productPropertyNameEditRequest.getProductPropertyValues().size() == 0) {

        } else { //新增属性名称
            //查询是否存在相同名称的属性
            List<ProductPropertyName> productPropertyNames = productPropertyNameMapper.queryByNameAndCode(productPropertyNameEditRequest.getName(),
                    "", productPropertyNameEditRequest.getAdminOperate().getDeptId());
            if (productPropertyNames.size() != 0) {
                result.setCode("9999");
                result.setMsg("存在相同的属性名");
                return result;
            }
            //查询是否存在相同编码的属性
            List<ProductPropertyName> productPropertyNamesByCode = productPropertyNameMapper.queryByNameAndCode("",
                    productPropertyNameEditRequest.getCode(),
                    productPropertyNameEditRequest.getAdminOperate().getDeptId());
            if (productPropertyNamesByCode.size() != 0) {
                result.setCode("9999");
                result.setMsg("存在相同的属性编码");
                return result;
            }
            productPropertyNameMapper.insert(productPropertyNameEditRequest);
            //新增属性值
            if (CollectionUtils.isNotEmpty(productPropertyNameEditRequest.getProductPropertyValues())) {
                for (ProductPropertyValueEditRequest propertyValue : productPropertyNameEditRequest.getProductPropertyValues()) {
                    propertyValue.setPropertyId(productPropertyNameEditRequest.getId());
                    propertyValue.setValueType(productPropertyNameEditRequest.getValueType());
                    productPropertyValueMapper.insert(propertyValue);
                }
            }
            result.setCode("0000");
            result.setMsg("调用成功");
        }
        return result;
    }

    /*
     *
     * 商品属性修改
     * */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResult update(ProductPropertyNameEditRequest productPropertyNameEditRequest) {
        RestResult restResult = new RestResult();
        if (productPropertyNameEditRequest.getProductPropertyValues().size() == 0) {
//            restResult.setMsg("属性值不能为空");
//            restResult.setCode("9999");
            return restResult;
        } else {
            //判断是否和已有的属性名相同
            ProductPropertyName productPropertyName = productPropertyNameMapper.queryByPrimaryKey(productPropertyNameEditRequest.getId());
            List<ProductPropertyName> productPropertyNames = productPropertyNameMapper.queryByNameAndCode(productPropertyNameEditRequest.getName(),
                    "", productPropertyNameEditRequest.getAdminOperate().getDeptId());
            if (productPropertyNames.size() == 1 && productPropertyNames.get(0).getName().equals(productPropertyNameEditRequest.getName())
                    && !productPropertyName.getName().equals(productPropertyNameEditRequest.getName())) {
                restResult.setCode("9999");
                restResult.setMsg("存在相同的属性名称");
                return restResult;
            }
            //查询是否存在相同编码的属性
            List<ProductPropertyName> productPropertyNamesByCode = productPropertyNameMapper.queryByNameAndCode("",
                    productPropertyNameEditRequest.getCode(), productPropertyNameEditRequest.getAdminOperate().getDeptId());
            if (productPropertyNamesByCode.size() == 1 && productPropertyNamesByCode.get(0).getCode().equals(productPropertyNameEditRequest.getCode())
                    && !productPropertyName.getCode().equals(productPropertyNameEditRequest.getCode())) {
                restResult.setCode("9999");
                restResult.setMsg("存在相同的属性编码");
                return restResult;
            }
            //修改属性名称
            productPropertyNameMapper.update(productPropertyNameEditRequest);
            //属性值表更新
            //先删除原有的属性值
            productPropertyNameMapper.deleteByPropertyId(productPropertyNameEditRequest.getId());
            //重新插入属性值
            if (CollectionUtils.isNotEmpty(productPropertyNameEditRequest.getProductPropertyValues())) {
                for (ProductPropertyValueEditRequest productPropertyValue : productPropertyNameEditRequest.getProductPropertyValues()) {
                    productPropertyValue.setPropertyId(productPropertyNameEditRequest.getId());
                    productPropertyValueMapper.insert(productPropertyValue);
                }
            }
            restResult.setCode("0000");
            restResult.setMsg("调用成功");
            return restResult;
        }

    }

    /*
     *
     * 商品属性删除
     * */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResult delete(String ids) {
        log.info("开始进入删除商品属性-------》" + ids);
        RestResult restResult = new RestResult();
        try {
            String strArr[] = ids.split(",");
            long[] strArrNum = (long[]) ConvertUtils.convert(strArr, long.class);
            for (int i = 0; i < strArrNum.length; i++) {
                List<CategoryPropertyName> categoryPropertyNameList = categoryPropertyNameMapper.queryByPropertyNameId(strArrNum[i]);
                if (categoryPropertyNameList.size() > 0) {
                    restResult.setMsg("属性值已在类目上被使用");
                    restResult.setCode("9999");
                } else {
                    //删除属性名
                    productPropertyNameMapper.delete(strArrNum[i]);
                    productPropertyNameMapper.deleteByPropertyId(strArrNum[i]);
                    restResult.setCode("0000");
                    restResult.setMsg("调用成功");
                }
            }


        } catch (Exception e) {
            log.error("delete", e);
//            restResult=RestResult.error("9999",e.getLocalizedMessage());
        }
        return restResult;
    }


    @Override
    public ProductPropertyName queryByPrimaryKey(Long id) {
        return productPropertyNameMapper.queryByPrimaryKey(id);
    }

    /*
     * 商品属性条件查询
     * */
    @Override
    public List<ProductPropertyName> query(ProductPropertyName productPropertyName) {
        return productPropertyNameMapper.query(productPropertyName);
    }

    /*
     *
     * 商品属性分页查询
     * */
    @Override
    public PageResult<ProductPropertyNameResponse> queryPage(ProductPropertyName productPropertyName, Page
            pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<ProductPropertyNameResponse> list = productPropertyNameMapper.queryPage(startIndex, pageQuery.getLimit(), productPropertyName);
        long count = productPropertyNameMapper.queryPageCount(productPropertyName);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }


    /*
     * 商品属性值条件查询
     * */
    @Override
    public List<ProductPropertyValue> queryPropertyValue(ProductPropertyValueRequest productPropertyValueRequest) {
        return productPropertyValueMapper.query(productPropertyValueRequest);
    }

    /*
     * 商品属性值条件查询
     * */
    @Override
    public ProductPropertyNameResponse queryPropertyValueById(ProductPropertyName productPropertyName) {
        return productPropertyNameMapper.queryPropertyValueById(productPropertyName);
    }

}
